# coding: utf-8

"""
@version: python3.6
@author: 噢啦嘿嘿
@contact: zc
@software: PyCharm
@file: App_Page.py
@time: 2021/5/20 15:36
"""
import time

from lib.init_data import get_data
import os
from lib.loggers import log
from lib.func import think_time
BASE_CONF = get_data(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config.ini"))['App']


class AppPage():
    def __init__(self,d):
        self.d = d
        self.app = BASE_CONF['PAGE_NAME']
        self.time_out = BASE_CONF['WAIT_TIMEOUT']
        self.lanuch_activityl = BASE_CONF['LANUCH_ACTIVITYl']


class BasePage(AppPage):
    def __init__(self, driver):
        self.driver = driver
        AppPage.__init__(self, self.driver)
        self.init_app()


#------------------------系统原生方法二次封装--------------------

    def init_app(self):
        #启动app
        self.d.app_start(self.app)


    def out_app(self,app):
        #停止app
        self.d.app_stop(app)


    def find_text(self, ele):
        #查询文本
        return self.d(text = ele)


    def find_class(self, ele):
        #查询class
        return self.d(text = ele)


    def find_id(self, ele):
        #查找id
        return self.d(resourceId = ele)


    def find_xpath(self,ele):
        #查找xpath
        return self.d(xpath=ele)


    def gettitle_text(self, ele):
        #根据UI元素的文本属性查找
        return ele.get_text()


    def input_text(self, ele, text):
        #根据元素输入
        ele.set_text(text)


    def back(self):
        #按返回键
        self.d.press("back")

    def loop_back(self,text):
        # 循环返回直到出现对应数据停止
        while not self.find_text(text):
            self.d.press("back")
            time.sleep(0.3)

    def long_click(self, element, duration=7, timeout=10):
        '''
        :param element:
        :param duration: 持续时间
        :param timeout: 超时时间
        :return:
        '''

        self.find_id(element).long_click(duration=duration, timeout=timeout)


#———————————————————点击、滑动、捕获totas、登录、退出等方法———————————————————————————————————————#

    #查找点击
    @think_time
    def text_click(self, text):
        #根据文本查找点击
        self.find_text(text).click()


    @think_time
    def id_click(self, id):
        #根据id查找点击
        self.find_id(id).click()


    @think_time
    def clss_click(self,classname):
        # 根据classname查找点击
        self.find_class(classname).click()


    @think_time
    def xpath_click(self,el):
        #根据xpath查找点击
        self.d.xpath(el).click()



    def sliding(self):
        #向上滑动整屏
        self.d(scrollable=True).fling()


    def scrollable_down(self):
        #向下滑动到顶部
        time.sleep(3)
        try:
            self.d.swipe_ext("down")
            time.sleep(1)
        except:
            raise
        finally:
            time.sleep(1)


    @think_time
    def scrollable_description(self, position):
        #根据文本滑动找到某个特定元素
        self.d(scrollable=True).scroll.to(text=position)


    # @think_time
    def get_text_text(self,text):
        #根据文本查找文本
        return self.gettitle_text(self.find_text(text))


    @think_time
    def get_xpath_id(self,id):
        #根据xpath查找id
        return self.find_xpath(self.find_id(id))


    @think_time
    def get_id_id(self, id):
        #根据id查找文本
        return self.gettitle_text(self.find_id(id))


    def input_login(self,text,id):
        #输入数据
        self.input_text(self.find_id(id), text)


    def msg(self):
        #捕获toast提示中的message
        mssa=self.d.toast.get_message()
        return mssa


    def backif(self,text):
        #查询文本，如果没有就返回
        if self.find_text(text):
            return
        else:
            self.back()


    def login(self,user,pwd):
        """
        :param user: 用户名
        :param pwd: 密码
        :return:
        """
        if self.find_text('微信一键登录'):
            self.d(text='使用手机号码登录').click()
            self.d(text='账号密码登录').click()
            self.input_login(user, 'com.truckhome.bbs:id/et_login_account')
            self.input_login(pwd, 'com.truckhome.bbs:id/et_login_pwd')
            self.d(resourceId="com.truckhome.bbs:id/iv_login_agree").click()
            self.d(resourceId='com.truckhome.bbs:id/iv_login_pwd').click()
        else:
            return


    def The_quit(self):
        #我的页面退出登录
        #公共退出没有写呢，后面优化或者谁写了就放在这下面也可以
        self.d(text="我的").click()
        if self.d(text='使用手机号码登录') != "使用手机号码登录":
            self.d(resourceId = "com.truckhome.bbs:id/iv_toolbar_setting").click()
            self.d(text="退出登录").click()
            self.d(text="确定").click()
            return "退出登录成功"



